package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.tests.GroupData;

public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public void initCreationContact() {
			click(By.linkText("add new"));
			
	}

	public void fillContactForm(ContactData contact) {
		type(By.name("firstname"), contact.name);
		type(By.name("lastname"), contact.lastname);
	  	type(By.name("address"), contact.mycity);
	  	type(By.name("home"), contact.homePhone);
	   	type(By.name("mobile"), contact.mobilePhone);
	    type(By.name("work"), contact.workPhone);
	    type(By.name("email"), contact.email);
		type(By.name("email2"), contact.email2);
		selectByText(By.name("bday"), contact.birthDay);
	    selectByText(By.name("bmonth"), contact.birthMonth);
		type(By.name("byear"), contact.birthYear);
	   	type(By.name("address2"), contact.address);
	   	type(By.name("phone2"), contact.homePhone2);
	}

	

	public void submitCreationContact() {
		click(By.name("submit"));
	}
	public void returnToHomepage() {
		click(By.linkText("home page"));
	}

	public void openEditContactPage(int index) {
		click(By.xpath("//table[@id='maintable']/tbody/tr["+index+"]/td[7]/a/img"));
	}

	public void deleteContactPage() {
		click(By.xpath("(//input[@name='update'])[2]"));
		
	}

	public void modificateContactForm() {
		click(By.xpath("(//input[@name='update'])"));
		
	}

	public List<ContactData> getContacts() {
		List<ContactData> contacts =  new ArrayList<ContactData>();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
				for (WebElement checkbox : checkboxes) {
			ContactData contact = new ContactData();
			String title = checkbox.getAttribute("title"); 
			contact.lastname = 	title.substring("Select (".length(), title.length() - ")".length());
			contacts.add(contact);
			
		}
		return contacts;
	}


}
